package cn.yeziji.forum.controller;

import cn.yeziji.forum.common.ForumResult;
import cn.yeziji.forum.dao.intf.ForumDetailsDao;
import cn.yeziji.forum.dto.details.ForumDetailDTO;
import cn.yeziji.forum.dto.details.ForumDetailPagingDTO;
import cn.yeziji.forum.dto.user.ForumUserDTO;
import cn.yeziji.forum.exception.DetailException;
import cn.yeziji.forum.common.enums.status.DetailsStatus;
import cn.yeziji.forum.utils.ForumResultUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 论坛文章控制器
 *
 * @author gzkemays
 * @since 2021/12/22 16:54
 */
@RestController
@RequestMapping("/details")
public class ForumDetailsController {
  @Resource ForumDetailsDao detailsDao;

  @PostMapping("/save")
  public ForumResult save(@RequestBody ForumDetailDTO dto) {
    int save = detailsDao.save(dto);
    if (save > 0) {
      if (dto.getTempSave()) {
        return ForumResultUtils.ok(save, DetailsStatus.DETAILS_TEMP_SAVE_SUCCESS.getMsg());
      } else {
        return ForumResultUtils.ok(save, DetailsStatus.DETAILS_PUBLISH_SUCCESS.getMsg());
      }
    }
    throw new DetailException(DetailsStatus.DETAILS_OPERATION_FAIL);
  }

  @GetMapping("/get")
  public ForumResult get(ForumDetailDTO dto) {
    return ForumResultUtils.ok(detailsDao.getOne(dto));
  }

  @GetMapping("/list")
  public ForumResult list(ForumDetailPagingDTO dto) {
    if (dto.getCollection() != null || dto.getGood() != null) {
      return ForumResultUtils.ok(detailsDao.getUserCollectionOrGoodDetails(dto));
    }
    return ForumResultUtils.ok(detailsDao.getList(dto));
  }

  /**
   * 返回当前用户的所有类型文章
   *
   * @param dto 分页传参
   * @return 用户的投稿箱
   * @deprecated 该接口已弃用，但可以充当测试，具体线上请使用 {@link #userList(ForumDetailPagingDTO)}
   */
  @GetMapping("/allListTest")
  public ForumResult allList(ForumDetailPagingDTO dto) {
    dto.setPage(1);
    dto.setLimit(99999);
    return ForumResultUtils.ok(detailsDao.getList(dto));
  }

  @GetMapping("/allList")
  public ForumResult userList(ForumDetailPagingDTO dto) {
    return ForumResultUtils.ok(detailsDao.getUserDetailList(dto));
  }

  @PostMapping("/delete")
  public ForumResult delete(@RequestBody ForumDetailDTO dto) {
    dto.setDelete(true);
    dto.setTempSave(true);
    return ForumResultUtils.ok(detailsDao.delOrRecovery(dto), "删除成功");
  }

  @PostMapping("/recovery")
  public ForumResult recovery(@RequestBody ForumDetailDTO dto) {
    dto.setDelete(false);
    dto.setTempSave(true);
    return ForumResultUtils.ok(detailsDao.delOrRecovery(dto), "恢复成功");
  }

  @GetMapping("/statistics")
  public ForumResult statistics(ForumUserDTO dto) {
    return ForumResultUtils.ok(detailsDao.getStatistics(dto));
  }
}
